package cn.huijielong.modules.oss.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.huijielong.modules.sys.controller.AbstractController;
import cn.huijielong.modules.oss.entity.OssFileEntity;
import cn.huijielong.modules.oss.service.OssFileService;
import cn.huijielong.common.utils.RD;
import cn.huijielong.common.utils.PageUtils;
import cn.huijielong.common.utils.Query;




/**
 * oss云文件（图片、文档）
 * 
 * @author xietong
 * @email xietong@gmail.com
 * @date 2018-04-02 13:48:46
 */
@RestController
@RequestMapping("/oss/ossfile")
public class OssFileController  extends AbstractController{
	@Autowired
	private OssFileService ossFileService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public RD list(@RequestParam Map<String, Object> params){
	    logger.info("从{}发送请求，参数：{}",getHeader("referer"),params.toString());
		//查询列表数据
        Query query = Query.createRequest(params);

		List<OssFileEntity> ossFileList = ossFileService.queryList((long) query.getInt("tableId"),query.getString("tableType"));
		int total = ossFileService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(ossFileList, total, query.getLimit(), query.getPage());
		
		return RD.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public RD info(@PathVariable("id") Long id){
		OssFileEntity ossFile = ossFileService.queryObject(id);
		
		return RD.ok().put("ossFile", ossFile);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody RD save(HttpServletRequest request, OssFileEntity ossFile){
		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
		ossFile.setCreatedBy(1010);
		ossFile.setUploadFile(files);
		ossFileService.save(ossFile);
		return RD.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public @ResponseBody RD update(HttpServletRequest request, OssFileEntity ossFile){
		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
		ossFile.setCreatedBy(1010);
		ossFile.setUploadFile(files);
		ossFileService.update(ossFile);
		return RD.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete/{tableType}/{tableId}")
	public RD delete(@PathVariable("tableType") String tableType,@PathVariable("tableId") Long tableId){
		ossFileService.delete(tableId, tableType);
		return RD.ok();
	}
	
}
